import pymysql
import datetime
import traceback

db = pymysql.connect(host="47.98.248.3", port=3307, user="root", password="123456", db="star_goods")
print(db)
cur = db.cursor()
try:
    now_datetime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    # 转换为datetime
    datetime_now_real = datetime.datetime.strptime(now_datetime, '%Y-%m-%d %H:%M:%S')
    sql = "insert into message_analysis (productId,pos_sum,neg_sum,get_real_time) values('{}',{},{},'{}')".format('6', 2, 3, datetime_now_real)
    print(sql)
    cur.execute(sql)
    db.commit()
    sql2 = "select * from message_analysis"
    cur.execute(sql2)
    print(cur.fetchall())  # 打印所有数据
except Exception:
    traceback.print_exc()
    print('插入失败，回滚~')
    db.rollback()
cur.close()
db.close()
